#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxd = 18+2;
const int MOD = 20020219;
const int INF = 0x3f3f3f3f;
int A[maxd];
int d[maxd][10][maxd];
ll l, r, n;
int lim[10];

int dp(int pos, int num, int cnt, bool flg){
    if(cnt > lim[num]) return 0;
    if(pos == 0) return 1;
    if(flg && d[pos][num][cnt]!=-1) return d[pos][num][cnt];
    int m = flg?9:A[pos];
    int ans = 0;
    for (int i = 0; i <= m; ++i) {
        if(i == num) ans = (ans + dp(pos-1, i, cnt+1, flg||i<m)) %MOD;
        else ans = (ans + dp(pos-1, i, 1, flg||i<m)) %MOD;
    }
    if(flg) d[pos][num][cnt] = ans;
    return ans;
}

int cal(ll x){
    int pos = 0;
    while(x)
    {
        A[++pos]=x%10;
        x/=10;
    }
    return dp(pos, 0, 0, false);
}

signed main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);
    int T; cin >> T;
    while (T--){
        memset(lim, INF, sizeof(lim));
        memset(d, -1, sizeof(d));
        memset(A, 0, sizeof A);
        cin >> l >> r >> n;
        for (int i = 0; i < n; ++i) {
            int num, cnt; cin >> num >> cnt;
            lim[num] = min(lim[num], cnt);
        }
        cout << (cal(r) - cal(l-1) + MOD)%MOD << endl;
    }


    return 0;
}